<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧内容 -->
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" @click="editclick('')"
                >新增角色</el-button
              >
            </el-row>
            <el-table border="" :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <!-- 作用域插槽? -->
                <template v-slot="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button size="small" type="primary" @click="editclick(row)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="del(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                :total="page.total"
                :page-size="page.pagesize"
                layout="prev, pager, next"
                :current-page="page.page"
                node-key="id"
                @current-change="handleCurrentChange"
              ></el-pagination
            ></el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 右侧内容 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              :closable="false"
              :show-icon="true"
            >
            </el-alert>
            <!-- 警告 -->
            <el-form
              :disabled="true"
              label-width="80px"
              style="margin-top: 15px"
              :model="companyInfo"
            >
              <el-form-item style="width: 400px" prop="name" label="公司名称">
                <el-input v-model="companyInfo.name"></el-input>
              </el-form-item>
              <el-form-item style="width: 400px" label="公司地址">
                <el-input v-model="companyInfo.companyAddress"></el-input>
              </el-form-item>
              <el-form-item style="width: 400px" label="邮箱地址">
                <el-input v-model="companyInfo.mailbox"></el-input>
              </el-form-item>
              <el-form-item
                style="width: 400px"
                prop="remarks"
                label="活动形式"
              >
                <el-input
                  type="textarea"
                  v-model="companyInfo.remarks"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <AddDept :isShow.sync="isShow" :editId="editId" ref="editref"></AddDept>
    </div>
    <!-- 放置弹层 -->
    <el-dialog
      title="分配权限"
      :visible="showPermDialog"
      @close="permCanclevtn"
    >
      <!-- 权限是一个树 -->
      <!-- id是唯一标识 -->
      <el-tree
        :data="permData"
        :props="defaultProps"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        :default-checked-keys="lightlist"
        node-key="id"
        ref="permData"
      ></el-tree>
      <el-row type="flex" justify="center">
        <el-col>
          <el-button type="primary" @click="addPermbtn">确定</el-button>
          <el-button @click="permCanclevtn">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  assignPerm
} from '@/api/setting'
import { mapGetters } from 'vuex'
import AddDept from './add-dept.vue'
import { getPermissionList } from '@/api/permission'
import { detailRoleList } from '@/api/role'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      isShow: false,
      list: [], //承接数组
      page: {
        page: 1, //  页数
        pagesize: 3, //  总条数
        total: 0 //  总数
      },
      roleId: '',
      permData: [], //  权限数据
      lightlist: [], //  默认选中
      defaultProps: {
        label: 'name'
      }, //
      editId: '',
      companyInfo: {},
      showPermDialog: false
    }
  },
  components: { AddDept },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleList(this.page)
      this.list = res.rows
      this.page.total = res.total
    },

    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(this.getCompanyID)
    },
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?').then(async () => {
        await deleteRole(id)

        this.getRoleList()
      })
    },
    async editclick(row) {
      this.isShow = true
      this.editId = row.id
      if (row.id != null) {
        //  如果有id  那么就执行子组的方法,进行数据回显
        this.$refs.editref.getRoleDetail(row.id)
      }
    },
    //  弹出层
    //   获取权限地啊你数据 点击时候调用
    async assignPerm(id) {
      this.showPermDialog = true
      this.roleId = id
      //   调取获取权限点数据
      this.permData = tranListToTreeData(await getPermissionList(), '0') //转化list 树形数据
      // console.log(this.permData)
      // 数据回显
      const res = await detailRoleList(id)
      // console.log(res)
      this.lightlist = res.permIds
    },
    // 确定就提交表单
    // dialog 确定
    async addPermbtn() {
      // console.log(this.$refs.permData.getCheckedKeys())
      // 获取选中数据
      // 调用接口
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permData.getCheckedKeys()
      })
      this.showPermDialog = false
      // 关闭弹层
    },
    // dialog'取消
    permCanclevtn() {
      this.lightlist = [] //  重置数据
      this.showPermDialog = false
    }
  },
  computed: {
    ...mapGetters(['getCompanyID'])
  }
}
</script>

